import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

const Login = () => import('../views/Login/Login.vue')
const Reg = () => import('@/views/Reg/Reg.vue')
const Main = () => import('../views/Main/Main.vue')
const Home = () => import('../views/Menus/Home/Home.vue')
const UserInfo = () => import('../views/Menus/User/UserInfo.vue')
const UserAvatar = () => import('../views/Menus/User/UserAvatar.vue')
const UserPwd = () => import('../views/Menus/User/UserPwd.vue')
const ArtCate = () => import('../views/Menus/Article/ArtCate.vue')
const ArtList = () => import('../views/Menus/Article/ArtList.vue')
Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login },
  { path: '/reg', component: Reg },
  {
    path: '/',
    component: Main,
    redirect: '/home',
    children: [
      { path: 'home', component: Home },
      { path: 'user-info', component: UserInfo },
      { path: 'user-avatar', component: UserAvatar },
      { path: 'user-pwd', component: UserPwd },
      { path: 'art-cate', component: ArtCate },
      { path: 'art-list', component: ArtList }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 全局前置守卫 - 拦截跳转
router.beforeEach((to, from, next) => {
  // console.log(store)
  const whileList = ['/login', '/reg']
  if (!store.state.user.token && !whileList.includes(to.path)) {
    return next('/login')
  }
  next()
})

export default router
